﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AngleExpress.utils
{
    class Utils
    {
        static public void ShowError(string errmsg)
        {
            MessageBox.Show(errmsg, "异常", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
        }

        static public void ShowWarn(string warnMsg)
        {
            MessageBox.Show(warnMsg, "警告", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
        }

        static public MessageBoxResult ShowQuestion(string msg)
        {
            return MessageBox.Show(msg, "提示", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
        }

        static public void ShowInfo(string msg)
        {
            MessageBox.Show(msg, "提示", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
        }

        public static string TraverseDir(string rootDir, string curDir, string filename, int depth = 1)
        {
            if (rootDir.Last() != '\\' || curDir.Last() != '\\')
            {
                throw new Exception("文件夹格式要以\\结束");
            }
            string reletivePath = curDir.Remove(0, rootDir.Length);
            int layer = reletivePath.Count(c => c == '\\') + 1;

            DirectoryInfo root = new DirectoryInfo(curDir);

            FileInfo[] files;
            try
            {
                files = root.GetFiles(filename);
            }
            catch (Exception)
            {
                return "";
            }

            if (files.Length > 0)
                return files[0].FullName;
            if (layer == depth)
            {
                return "";
            }
                

            foreach (DirectoryInfo subdir in root.GetDirectories())
            {
                string result = TraverseDir(rootDir, subdir.FullName + "\\", filename, depth);
                if (result != "")
                    return result;
            }

            return "";
        }
    }
}
